package gui;

import java.util.concurrent.CountDownLatch;

import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import details.User;

public class ChangePassDialog extends Dialog {
	private static Display display;
	private static ChangePassDialog inst;
	private static Shell shell;
	private static Shell dialogShell;
	private static Button logDialogButtonExit;
	private static Button logDialogButtonStart;
	private static Group logDialogGroup;
	private static Text logUserText;
	private static Text logPassText;
	private static Text logPass2Text;
	public static boolean successful = false;

	static Image background = new Image(display,
			Main.class.getResourceAsStream("changePass-back.png"));

	public static void openLogDialog() {
		try {
			display = Display.getDefault();
			shell = new Shell(display);
			shell.setLocation(Main.getCentral_x() - 306,
					Main.getCentral_y() - 358);
			shell.setSize(900, 800);
			shell.setBackgroundImage(background);
			inst = new ChangePassDialog(shell, 0);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeInitDialog() {
		shell.close();
	}

	public ChangePassDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, 67680);

			dialogShell.setText("Music Box - Login");
			dialogShell.setBackgroundImage(background);
			dialogShell.setLayout(null);

			logDialogGroup = new Group(dialogShell, 0);
			logDialogGroup.setLayout(null);
			logDialogGroup.setBackgroundImage(background);
			logDialogGroup.setBounds(0, -8, 380, 300);

			logUserText = new Text(logDialogGroup, 2048);
			logUserText.setBounds(220, 140, 125, 24);

			logPassText = new Text(logDialogGroup, 2048);
			logPassText.setBounds(220, 172, 125, 24);
			logPassText.setEchoChar('*');

			logPass2Text = new Text(logDialogGroup, 2048);
			logPass2Text.setBounds(220, 204, 125, 24);
			logPass2Text.setEchoChar('*');

			logDialogButtonStart = new Button(logDialogGroup, 16777224);

			logDialogButtonStart.setText("Change");
			logDialogButtonStart.setBounds(75, 250, 110, 40);
			logDialogButtonStart.addListener(13, new Listener() {
				public void handleEvent(Event event) {
					String user = ChangePassDialog.logUserText.getText();
					String pass = ChangePassDialog.logPassText.getText();
					String pass2 = ChangePassDialog.logPass2Text.getText();

					if (user == "") {
						MessageBox messageBox = new MessageBox(
								ChangePassDialog.shell, 296);

						messageBox.setMessage("Enter the User Name");
						messageBox.open();
					}
					if (pass == "") {
						MessageBox messageBox = new MessageBox(
								ChangePassDialog.shell, 296);

						messageBox.setMessage("Enter the Password");
						messageBox.open();
					}
					if (pass2 == "") {
						MessageBox messageBox = new MessageBox(
								ChangePassDialog.shell, 296);

						messageBox.setMessage("Enter the Password");
						messageBox.open();
					} else {
						Main.signal = new CountDownLatch(1);
						Main.dbGui.changePassword(user, pass, pass2);
						try {
							Main.signal.await();
						} catch (InterruptedException e) {
							MessageBox messageBox = new MessageBox(
									ChangePassDialog.shell, 32);
							messageBox.setText("Error");
							messageBox.setMessage("Error: "
									+ e.getLocalizedMessage());
							messageBox.open();
						}
						
						String guiErrorMsg = Main.guiInt.getMassage();
						System.out.println(guiErrorMsg + "login class");
						if (guiErrorMsg!=null){
							MessageBox messageBox = new MessageBox(
									ChangePassDialog.shell, 32);
							messageBox.setText("Password Change");
							messageBox.setMessage("Error: "
									+ guiErrorMsg);
							messageBox.open();
						} else{
							User mainUser = Main.getUser(); 
							mainUser.setPassword(pass2);
							Main.setUser(mainUser);
							MessageBox messageBox = new MessageBox(
									ChangePassDialog.shell, 32);
							messageBox.setText("Password Change");
							messageBox
									.setMessage("Password changed seccessfuly");
							messageBox.open();
							ChangePassDialog.successful = true;
							ChangePassDialog.shell.close();
						}
					}
				}
			});
			logDialogButtonExit = new Button(logDialogGroup, 16777224);

			logDialogButtonExit.setText("Cancel");
			logDialogButtonExit.setBounds(190, 350, 110, 40);
			logDialogButtonExit.addListener(13, new Listener() {
				public void handleEvent(Event event) {
					ChangePassDialog.shell.close();
				}
			});
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();

			while (!dialogShell.isDisposed())
				if (!display.readAndDispatch())
					display.sleep();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Shell getDialogShell() {
		return dialogShell;
	}

	public static void setDialogShell(Shell dialogShell) {
		dialogShell = dialogShell;
	}

	public static Button getInitDialogButtonExit() {
		return logDialogButtonExit;
	}

	public static void setInitDialogButtonExit(Button logDialogButtonExit) {
		logDialogButtonExit = logDialogButtonExit;
	}

	public static Button getInitDialogButtonStart() {
		return logDialogButtonStart;
	}

	public static void setInitDialogButtonStart(Button logDialogButtonStart) {
		logDialogButtonStart = logDialogButtonStart;
	}

	public static Group getInitDialogGroup() {
		return logDialogGroup;
	}

	public static void setInitDialogGroup(Group logDialogGroup) {
		logDialogGroup = logDialogGroup;
	}

	public static Display getInitDisplay() {
		return display;
	}

	public static void setDisplay(Display display) {
		display = display;
	}

	public static Display getDisplay() {
		return display;
	}

	public static ChangePassDialog getInst() {
		return inst;
	}

	public static Shell getShell() {
		return shell;
	}
}